c# constructor call another constructor

109

c# call constructor from constructor -

using System;

namespace call_another_constructor
{
    class sample
    {
        public sample()
        {
            Console.WriteLine("Constructor 1");
        }
        public sample(int x): this()
        {
            Console.WriteLine("Constructor 2, value: {0}",x);
        }
        public sample(int x, int y): this(x)
        {
            Console.WriteLine("Constructor 3, value1: {0} value2: {1}", x, y);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            sample s1 = new sample(12, 13);
        }
    }
}

Comments

Submit
0 Comments